<template>
  <div class="addresss">
    <van-sticky>
      <van-nav-bar :title="$route.meta.title" left-arrow @click-left="onClickLeft" style="width:100%;" />
    </van-sticky>
    <van-address-list v-model="chosenAddressId" :list="lists" @select='changeaddress' default-tag-text="默认" @add="addAddress" @edit="showAddress" />

    <van-empty description="暂无地址" v-if="lists.length == 0" />
    <!-- 详细地址 -->
    <van-popup v-model="show" position="bottom" :style="{ height: '65%' }" closeable round>
      <van-address-edit :address-info="addressInfo" :area-list="areaList" :show-delete="buttonshow" show-set-default @save="onSave" @delete="onDelete" @change-area="changeArea" />
    </van-popup>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { Toast } from "vant";
export default {
  name: "MyAddress",
  data () {
    return {
      id: "",
      areaList,
      nowareaList: [],
      show: false,
      searchResult: [],
      chosenAddressId: "1",
      //地址列表
      lists: [],
      //新建地址
      addressInfo: {},
      buttonshow: "",
      changeid: "",
      type: "",
    };
  },
  created () {
    this.type = this.$route.query.type;
    this.getMyAddress();
  },
  methods: {
    changeaddress (item) {
      if (this.type == 1) {
        localStorage.setItem('address', JSON.stringify(item))
        this.$router.go(-1);
      }
    },
    onClickLeft () {
      this.$router.go(-1);
    },
    changeArea (val) {
      this.nowareaList = val;
    },
    addAddress () {
      this.show = true;
      this.buttonshow = false;
      this.addressInfo = {};
    },
    showAddress (item) {
      this.changeid = item.id;
      this.addressInfo = this.lists.filter((v) => {
        return item.id == v.id;
      })[0];
      //delbtn： true是编辑，false是新建
      this.show = true;
      this.buttonshow = true;
    },
    onSave (v) {
      //新建地址
      if (this.buttonshow == false) {
        this.$axios("/buy/createmyaddress", {
          user_name: v.name, //用户名
          phone: v.tel, //联系电话
          province: v.province, //省
          city: v.city, //市
          district: v.county, //区
          address: v.addressDetail, //详细地址
          defaultadd: v.isDefault == true ? 1 : 2, //是否默认 1-默认，2-非默认,
          areaCode: v.areaCode,
        }).then((res) => {
          if (res.data.code == 1) {
            Toast.success("添加成功");
            this.getMyAddress();
            this.show = false;
          } else {
            Toast.fail("添加失败");
          }
        });
      } else {
        //编辑地址
        this.$axios("/buy/createmyaddress", {
          id: this.changeid, //地址id
          user_name: v.name, //用户名
          phone: v.tel, //联系电话
          province: v.province, //省
          city: v.city, //市
          district: v.county, //区
          address: v.addressDetail, //详细地址
          defaultadd: v.isDefault == true ? 1 : 2, //是否默认 1-默认，2-非默认,
          areaCode: v.areaCode,
        }).then((res) => {
          if (res.data.code == 1) {
            Toast.success("修改成功");
            this.getMyAddress();
            this.show = false;
          } else {
            Toast.fail("修改失败");
          }
        });
      }
    },
    onDelete () {
      //删除地址
      this.$axios("/buy/delmyaddress", {
        id: this.changeid, //地址ID
      }).then((res) => {
        if (res.data.code == 1) {
          Toast.success("删除成功");
          this.show = false;
          this.getMyAddress();
        } else {
          Toast.fail("删除失败");
        }
      });
    },
    getMyAddress () {
      this.$axios("/buy/getmyaddresslist").then((res) => {
        if (res.data.code == 1) {
          this.lists = res.data.data.map((v) => {
            return {
              ...v,
              name: v.username,
              tel: v.phone,
              address:
                v.province + v.city + v.district + v.address,
              addressDetail: v.province + v.city + v.district,
              county: v.district,
              isDefault: v.defaultadd == 1,
              areaCode: JSON.stringify(v.areaCode),
            };
          });
        } else {
          Toast.fail("加载失败");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.addresss {
  /deep/.van-button--danger {
    border: 0;
    background-color: #1bb89c;
  }
  /deep/.van-switch--on {
    background-color: #1bb89c;
  }
  /deep/.van-address-item .van-radio__icon--checked .van-icon {
    border: 1px solid #1bb89c;
    background-color: #1bb89c;
  }
}
</style>